package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISocWebsiteDAO;
import org.jsola.hr.dto.SocWebsiteAddDTO;
import org.jsola.hr.dto.SocWebsiteUpdateDTO;
import org.jsola.hr.entity.SocWebsiteDO;
import org.jsola.hr.query.SocWebsiteQuery;
import org.jsola.hr.service.ISocWebsiteService;
import org.jsola.hr.vo.SocWebsiteListVO;
import org.jsola.hr.vo.SocWebsiteVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 社保网站与类型
 *
 * @author zhr
 */
@Service("hrSocWebsiteServiceImpl")
public class SocWebsiteServiceImpl implements ISocWebsiteService {

    @Autowired
    private ISocWebsiteDAO socWebsiteDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SocWebsiteVO save(SocWebsiteAddDTO socWebsiteAddDTO, TokenUser tokenUser) {
        // 转DO
        SocWebsiteDO socWebsiteDO = socWebsiteAddDTO.to(SocWebsiteDO.class);
        // 保存
        socWebsiteDO = save(socWebsiteDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return socWebsiteDO.to(SocWebsiteVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SocWebsiteUpdateDTO socWebsiteUpdateDTO, TokenUser tokenUser) {
        //转DO
        SocWebsiteDO socWebsiteDO = socWebsiteUpdateDTO.to(SocWebsiteDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(socWebsiteDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... socWebsiteIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) socWebsiteIds);
    }


    @Override
    public SocWebsiteVO selectById(Long socWebsiteId, String siteId) {
        SocWebsiteDO socWebsiteDO = selectDOById(socWebsiteId, siteId);
        if (socWebsiteDO == null) {
            return null;
        }
        return socWebsiteDO.to(SocWebsiteVO.class);
    }

    @Override
    public List<SocWebsiteListVO> select(SocWebsiteQuery socWebsiteQuery, String siteId) {
        List<SocWebsiteDO> socWebsiteDOList = selectDO(socWebsiteQuery, siteId);
        if(CollectionUtils.isEmpty(socWebsiteDOList)) {
            return socWebsiteDOList == null ? null : new ArrayList<>();
        }
        return socWebsiteDOList.stream()
                .map(socWebsiteDO -> socWebsiteDO.to(SocWebsiteListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SocWebsiteQuery socWebsiteQuery, String siteId) {
        Example example = buildExample(socWebsiteQuery,siteId);
        return socWebsiteDAO.selectCountByExample(example);
    }

    @Override
    public Page<SocWebsiteListVO> selectPage(SocWebsiteQuery socWebsiteQuery, String siteId) {
        Example example = buildExample(socWebsiteQuery,siteId);
        Page<SocWebsiteDO> page = socWebsiteDAO.selectPageByExample(example,
                socWebsiteQuery.getPageNo(),
                socWebsiteQuery.getPageSize());

        return page.to(SocWebsiteListVO.class);
    }

    @Override
    public SocWebsiteDO selectDOById(Long socWebsiteId, String siteId) {
        return listById(socWebsiteId, siteId);
    }

    @Override
    public List<SocWebsiteDO> selectDO(SocWebsiteQuery socWebsiteQuery, String siteId) {
        Example example = buildExample(socWebsiteQuery,siteId);
        return socWebsiteDAO.selectByExample(example);
    }

    @Override
    public List<SocWebsiteDO> listAllNotSitId(Boolean isDistinct){
        Example example = new Example(SocWebsiteDO.class);
        example.and()
                .andEqualTo("valid", true);
        List<SocWebsiteDO> socWebsiteList = socWebsiteDAO.selectByExample(example);
        if(isDistinct){
            // 城市去重
            return socWebsiteList.parallelStream().collect(Collectors.collectingAndThen
                    (Collectors.toCollection(() ->
                                    new TreeSet<>(Comparator.comparing(SocWebsiteDO::getAreaName))),
                            ArrayList::new
                    )
            );
        } else {
            return socWebsiteList;
        }
    }

    @Override
    public List<SocWebsiteDO> selectWebSiteByAreaId(String areaId){
        Example example = new Example(SocWebsiteDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("areaId",Long.parseLong(areaId));
        return socWebsiteDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param socWebsiteQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SocWebsiteQuery socWebsiteQuery, String siteId) {
        Example example = new Example(SocWebsiteDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,socWebsiteQuery.getOrders());
        return example;
    }
}




